
//#include  <msp430x54xA.h>
#include  "Drivers.h"
#include  "..\ESS_CommonDefine.h"
////#include "..\SystemConfigDefineByFlash.h"
#include "..\ESS_MainSystemConfigDefine.h"

unsigned char G_Device_TimerB_Controls;
unsigned char G_Device_TimerB_Status;


static unsigned char blinking_counter;    //for LED Blinking

static unsigned int DSGOverCurrentCounting;   //for DSG OverCurrent Protection counting
static unsigned int CHGOverCurrentCounting;   //for CHG OverCurrent Protection counting
static unsigned int BalancingCounting;        //for Balancing counting
//static unsigned char CHG_Mos_TurnOff_Stable_counter;    //when CHG mosfet trun off, start count to stable time for checking mosfet is whether failure.
//static unsigned char DSG_Mos_TurnOff_Stable_counter;    //when DSG mosfet trun off, start count to stable time for checking mosfet is whether failure.
////////////////////////////////////////////////////////////////////////
//Watch Dog function counting Timer
static unsigned char WatchDogStartFlag = 0;;
static void EmptyWatchDogFunc();
void (*WatchDog_ptr_func)();
static unsigned int WatchDog_delay_count, setWatchDogCountingCount;

////////////////////////////////////////////////////////////////////////
//ACLK = n/a, MCLK = SMCLK = TBCLK = default DCO ~1.045MHz
void InitTimerB(){

  TBCCR0 = 52700;                           // ~50ms,  1ms = 1054 clk, //error: 1ms = 527 clk
  //TBCCR0 = Timer_B_CCR0;                           // ~50ms,  1ms = 1054 clk, //error: 1ms = 527 clk
  TBCTL = TBSSEL_2 + MC_1 + TBCLR;          // SMCLK, upmode, clear TBR
  TBCCTL0 |= CCIE;                          // TBCCR0 interrupt enabled
  
  ///////////////////////////////////////////////////////////////////////
  //initial values
  G_Device_TimerB_Controls = 0;
  G_Device_TimerB_Status = 0;
  blinking_counter = 0;
  DSGOverCurrentCounting = 0;
  CHGOverCurrentCounting = 0;
  BalancingCounting = 0;
//  CHG_Mos_TurnOff_Stable_counter = 0;
//  DSG_Mos_TurnOff_Stable_counter = 0;
  
  
  ///////////////////////////////////////////////////////////////////////
  //Watch Dog function counting Timer
  WatchDog_ptr_func = EmptyWatchDogFunc;
  WatchDogStartFlag = false;
  WatchDog_delay_count = 0;
  setWatchDogCountingCount = 0;
}
void StopTimerB(){
  TBCCTL0 &= ~CCIE;                          // CCR0 interrupt disabled
  TBCTL = MC_0 + TACLR;         // Stop mode: Timer is halted, clear TAR
  
}


////////////////////////////////////////////////////
//DSG Over Current counting
void setDSGOverCurrentCounting(unsigned char enable){
  DSGOverCurrentCounting = 0;
  G_Module_Active_Status &= ~DSG_OCP_COUNT_FINISH;
  if(enable){
    G_Module_Active_Status |= DSG_OCP_COUNT_START;
  }else{
    G_Module_Active_Status &= ~DSG_OCP_COUNT_START;
  }
}
////////////////////////////////////////////////////
//CHG Over Current counting
void setCHGOverCurrentCounting(unsigned char enable){
  CHGOverCurrentCounting = 0;
  G_Module_Active_Status &= ~CHG_OCP_COUNT_FINISH;
  if(enable){
    G_Module_Active_Status |= CHG_OCP_COUNT_START;
  }else{
    G_Module_Active_Status &= ~CHG_OCP_COUNT_START;
  }
}

////////////////////////////////////////////////////
//CHG Over Current counting
void setBalancingCounting(unsigned char enable){
  BalancingCounting = 0;
  G_Module_Active_Status &= ~BALANCING_COUNT_FINISH;
  if(enable){
    G_Module_Active_Status |= BALANCING_COUNT_START;
  }else{
    G_Module_Active_Status &= ~BALANCING_COUNT_START;
  }
}

////////////////////////////////////////////////////
//Watch Dog function
void setWatchDogTimerToRunFunc(void (*watchDogPtr)(), unsigned int delay50ms_count){
  WatchDogStartFlag = false;
  WatchDog_ptr_func = watchDogPtr;
  setWatchDogCountingCount = delay50ms_count;
  WatchDog_delay_count = 0;
}
static void EmptyWatchDogFunc(){
}
void StartWatchDog(){
  WatchDog_delay_count = 0;
  WatchDogStartFlag = true;
}
void StopWatchDog(){
  WatchDogStartFlag = false;
  WatchDog_delay_count = 0;
}

void DisableWatchDogFunc(){
  WatchDogStartFlag = false;
  WatchDog_ptr_func = EmptyWatchDogFunc;
  WatchDog_delay_count = 0;
}


// Timer B0 interrupt service routine (CCIE for CCR0)
#pragma vector=TIMERB0_VECTOR
__interrupt void TIMERB0_ISR (void)
{
  
  ////////////////////////////////////////
  // LED Blink section  (start)
  if(G_Device_Action_Controls & LED_BLINK){
    blinking_counter++;
    if(blinking_counter >= LED_Blinking_Interval_Time){
      Blink_LEDs_For_Timer();
      blinking_counter = 0;
    }
  }else{
    Light_OnOff_LEDs_For_Timer();
  }
  // LED Blink section  (stop)
  ////////////////////////////////////////
  
  ////////////////////////////////////////
  // Button section  (start)
  if(G_Device_Action_Controls & BUTTON_PRESS){
    if(getSW1Status()){
      if(ButtonPressCounter >= ButtonPressTime){
        G_Device_Action_Controls |= BUTTON_LONG_PRESS;
        G_Device_Action_Controls &= ~BUTTON_PRESS;
        ButtonPressCounter = 0;
      }else{
        ButtonPressCounter++;
      }
    }else{
      G_Device_Action_Controls |= BUTTON_CLICK;
      G_Device_Action_Controls &= ~BUTTON_PRESS;
      ButtonPressCounter = 0;
    }
  }
  // Button section  (stop)
  ////////////////////////////////////////
  ////////////////////////////////////////
  // DSG OCP count section  (start)
  if(G_Module_Active_Status & DSG_OCP_COUNT_START){
    if(DSGOverCurrentCounting >= SystemCurrentProtection_ReleaseTime){
      G_Module_Active_Status |= DSG_OCP_COUNT_FINISH;
      G_Module_Active_Status &= ~DSG_OCP_COUNT_START;
      DSGOverCurrentCounting = 0;
    }else{
      DSGOverCurrentCounting++;
    }
  }
  // DSG OCP count section  (stop)
  ////////////////////////////////////////
  ////////////////////////////////////////
  // CHG OCP count section  (start)
  if(G_Module_Active_Status & CHG_OCP_COUNT_START){
    if(CHGOverCurrentCounting >= SystemCurrentProtection_ReleaseTime){
      G_Module_Active_Status |= CHG_OCP_COUNT_FINISH;
      G_Module_Active_Status &= ~CHG_OCP_COUNT_START;
      CHGOverCurrentCounting = 0;
    }else{
      CHGOverCurrentCounting++;
    }
  }
  // CHG OCP count section  (stop)
  ////////////////////////////////////////

  ////////////////////////////////////////
  // BALANCING_COUNT_START, count section  (start)
  if(G_Module_Active_Status & BALANCING_COUNT_START){
    if(BalancingCounting >= BalancingTurnOnTime){
      G_Module_Active_Status |= BALANCING_COUNT_FINISH;
      G_Module_Active_Status &= ~BALANCING_COUNT_START;
      BalancingCounting = 0;
      setBalanceChannel(Disable_Balancing);
    }else{
      BalancingCounting++;
    }
  }
  // BALANCING_COUNT_START, count section  (stop)
  ////////////////////////////////////////
//  ////////////////////////////////////////
//  // CHG mosfet trun off,count to stable time , count section  (start)
//  if( G_Device_Action_Controls & CHG_MOSFET_STATUS){
//    if(((G_Device_Action_Controls & CHG_MOS_TURNOFF_STABLE) == 0) && (CHG_Mos_TurnOff_Stable_counter > MOSFET_Stable_Count )){
//      G_Device_Action_Controls |= CHG_MOS_TURNOFF_STABLE;
//      CHG_Mos_TurnOff_Stable_counter = 0;
//    }else{
//      CHG_Mos_TurnOff_Stable_counter++;
//    }
//  }else{
//    G_Device_Action_Controls &= ~CHG_MOS_TURNOFF_STABLE;
//    CHG_Mos_TurnOff_Stable_counter = 0;
//  }
//  // CHG mosfet trun off,count to stable time , count section  (stop)
//  ////////////////////////////////////////
//  ////////////////////////////////////////
//  // DSG mosfet trun off,count to stable time , count section  (start)
//  if( G_Device_Action_Controls & DSG_MOSFET_STATUS){
//    if(((G_Device_Action_Controls & DSG_MOS_TURNOFF_STABLE) == 0) && (DSG_Mos_TurnOff_Stable_counter > MOSFET_Stable_Count )){
//      G_Device_Action_Controls |= DSG_MOS_TURNOFF_STABLE;
//      DSG_Mos_TurnOff_Stable_counter = 0;
//    }else{
//      DSG_Mos_TurnOff_Stable_counter++;
//    }
//  }else{
//    G_Device_Action_Controls &= ~DSG_MOS_TURNOFF_STABLE;
//    DSG_Mos_TurnOff_Stable_counter = 0;
//  }
//  // DSG mosfet trun off,count to stable time , count section  (stop)
//  ////////////////////////////////////////
  ////////////////////////////////////////////////////
  //Watch Dog function, count section  (start)
  if(WatchDogStartFlag ){
    if(WatchDog_delay_count++ >= setWatchDogCountingCount){
      (*WatchDog_ptr_func)();
      WatchDogStartFlag = false;
      WatchDog_delay_count = 0;
    }
  }else{
      WatchDog_delay_count = 0;
  }  
  //Watch Dog function, count section  (stop)
  ////////////////////////////////////////////////////
  
}//_interrupt void TIMERB0_ISR (void)



